<template>
    <el-dialog :title="!replyForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
      <el-form :model="replyForm" :rules="replyRule" ref="replyForm" @keyup.enter.native="replyFormSubmit()" label-width="80px">
        <el-form-item label="回复内容" prop="replyContent">
          <el-input v-model="replyForm.replyContent" placeholder="回复内容" type="textarea"></el-input>
        </el-form-item>
        <el-form-item label="回复人" prop="replyUser">
          <el-input v-model="replyForm.replyUser" placeholder="回复人"></el-input>
        </el-form-item>
        <el-form-item label="回复时间" prop="replyTime">
          <el-date-picker v-model="replyForm.replyTime" type="datetime" placeholder="回复时间"></el-date-picker>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="replyFormSubmit()">确定</el-button>
      </span>
    </el-dialog>
  </template>
  
  <script>
  export default {
    data() {
      return {
        visible: false,
        replyForm: {
          id: 0,
          replyContent: undefined,
          replyUser: undefined,
          replyTime: undefined,
        },
        replyRule: {
          replyContent: [{ required: true, message: "回复内容不能为空", trigger: "blur" }],
          replyUser: [{ required: true, message: "回复人不能为空", trigger: "blur" }],
          replyTime: [{ required: true, message: "回复时间不能为空", trigger: "blur" }],
        },
      };
    },
    methods: {
      init(id) {
        this.replyForm.id = id || 0;
        this.visible = true;
        this.$nextTick(() => {
          this.$refs["replyForm"].resetFields();
          if (this.replyForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/admin/reply/info/${this.replyForm.id}`),
              method: "get",
            }).then(({ data }) => {
              if (data && data.code === 0) {
                const info = data.result;
                this.replyForm.replyContent = info.replyContent;
                this.replyForm.replyUser = info.replyUser;
                this.replyForm.replyTime = info.replyTime;
              }
            });
          }
        });
      },
      // 表单提交
      replyFormSubmit() {
        this.$refs["replyForm"].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/admin/reply/${!this.replyForm.id ? "save" : "update"}`),
              method: "post",
              data: this.$http.adornData({
                replyContent: this.replyForm.replyContent,
                replyUser: this.replyForm.replyUser,
                replyTime: this.replyForm.replyTime,
              }),
            }).then(({ data }) => {
              if (data && data.code === 0) {
                this.$message({
                  message: "操作成功",
                  type: "success",
                  duration: 1500,
                  onClose: () => {
                    this.visible = false;
                    this.$emit("refreshDataList");
                  },
                });
              } else {
                this.$message.error(data.msg);
              }
            });
          }
        });
      },
    },
  };
  </script>
  